﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace End_term
{
    public partial class Register : Form
    {
        public Register()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "")
                MessageBox.Show("用户名和密码不能为空！");
            else
            {
                SqlConnection conn = new SqlConnection("server=LAPTOP-94P4A8KT;uid=sa;pwd=123456;database=Blogs");
                conn.Open();

                SqlCommand cmd = new SqlCommand("select * from Users where Username='" + textBox1.Text.Trim() + "'", conn);

                SqlDataReader sdr = cmd.ExecuteReader();

                sdr.Read();

                if (sdr.HasRows)
                    MessageBox.Show("该用户已注册，请使用其他用户名");
                else
                {
                    sdr.Close();
                    string insert1 = "insert into Users(Username,Password) values ('" + textBox1.Text + "','" + textBox2.Text + "')";
                    var name1 = textBox1.Text;
                    var pwd1 = textBox2.Text;
                    var pwd2 = textBox3.Text;
                    if (name1 == null && pwd1 == null)
                    {
                        MessageBox.Show("用户名和密码不能为空");
                    }
                    else
                    {
                        if (pwd1 != pwd2)
                        {
                            MessageBox.Show("两次输入的密码不一致!");
                        }
                        else
                        {
                    SqlCommand mycom = new SqlCommand(insert1, conn);
                    mycom.ExecuteNonQuery();
                    conn.Close();
                    conn.Dispose();
                    MessageBox.Show("注册成功！");
                    this.Close();
                        }
                    }

                    
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
